package com.fzy.core;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Page {
	private static final Log log = LogFactory.getLog(Page.class);
	public static final int DEFAULT_PAGE_SIZE = 10;
	public static final int DEFAULT_PAGE_NO = 1;
	
	public static final String ASC = "asc";
	public static final String DESC = "desc";

	protected int pageNo = DEFAULT_PAGE_NO;
	protected int pageSize = DEFAULT_PAGE_SIZE;
	protected String orderBy = null;
	protected String order = null;
	protected boolean autoCount = true;

	protected List result = new ArrayList();
	protected long totalCount = -1;

	public Page() {
	}

	public Page(int pageSize) {
		this.pageSize = pageSize;
	}

	public Page(PageForm pageForm) {
		initFromPageForm(pageForm);
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(final int pageNo) {
		this.pageNo = pageNo;
		correctPageNoByTotalCount();
		if (pageNo < 1) {
			this.pageNo = DEFAULT_PAGE_NO;
		}
		
	}

	public Page pageNo(final int thePageNo) {
		setPageNo(thePageNo);
		return this;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(final int pageSize) {
		this.pageSize = pageSize;
		if(this.pageSize < 1) this.pageSize = DEFAULT_PAGE_SIZE;
		correctPageNoByTotalCount();
	}

	public Page pageSize(final int thePageSize) {
		setPageSize(thePageSize);
		return this;
	}

	public int getFirst() {
		return ((pageNo - 1) * pageSize) + 1;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(final String orderBy) {
		this.orderBy = orderBy;
	}

	public Page orderBy(final String theOrderBy) {
		setOrderBy(theOrderBy);
		return this;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(final String order) {
		if(order == null) return;
		String lowcaseOrder = StringUtils.lowerCase(order);

		String[] orders = StringUtils.split(lowcaseOrder, ',');
		for (int i = 0; i < orders.length; i++) {
			String orderStr = orders[i];
			if (!StringUtils.equals(DESC, orderStr) && !StringUtils.equals(ASC, orderStr)) {
				throw new IllegalArgumentException("order " + orderStr + " is illegal");
			}
		}
		this.order = lowcaseOrder;
	}

	public Page order(final String theOrder) {
		setOrder(theOrder);
		return this;
	}

	public boolean isOrderBySetted() {
		return (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(order));
	}

	public boolean isAutoCount() {
		return autoCount;
	}

	public void setAutoCount(final boolean autoCount) {
		this.autoCount = autoCount;
	}

	public Page autoCount(final boolean theAutoCount) {
		setAutoCount(theAutoCount);
		return this;
	}

	public List getResult() {
		return result;
	}

	public void setResult(final List result) {
		this.result = result;
	}

	public long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(final long totalCount) {
		this.totalCount = totalCount;
		correctPageNoByTotalCount();
	}

	public long getTotalPages() {
		if (totalCount < 0) {
			return 0;
		}

		long count = totalCount / pageSize;
		if (totalCount % pageSize > 0) {
			count++;
		}
		return count;
	}

	public boolean isHasNext() {
		return (pageNo + 1 <= getTotalPages());
	}

	public int getNextPage() {
		if (isHasNext()) {
			return pageNo + 1;
		} else {
			return pageNo;
		}
	}

	public boolean isHasPre() {
		return (pageNo - 1 >= 1);
	}

	public int getPrePage() {
		if (isHasPre()) {
			return pageNo - 1;
		} else {
			return pageNo;
		}
	}
	
	public void initFormList(List list){
		int fromIndex = getFirst() - 1;
		int toIndex = getFirst() + getPageSize() - 1;
		toIndex = toIndex > list.size() ? list.size():toIndex;
		if(list != null && list.size() >= fromIndex){
			setTotalCount(list.size());
			setResult(list.subList(fromIndex,toIndex));
		}
	}
	
	public void initFromPageForm(PageForm pageForm){
		if(pageForm == null) return ;
		setPageSize(U.string2int(pageForm.getPageSize(), DEFAULT_PAGE_SIZE));
		setPageNo(U.string2int(pageForm.getPageNo(), DEFAULT_PAGE_NO));
		setOrder(pageForm.getOrder());
		setOrderBy(pageForm.getOrderBy());
	}
	
	public boolean isParamCorrect(){
		return pageNo > 0 && pageSize > 0;
	}
	
	private void correctPageNoByTotalCount(){
		//oracle bug
		if(totalCount > -1 && isParamCorrect()){
			if(getFirst() > totalCount){
				pageNo = (int)(totalCount / pageSize);
				if(getFirst() + pageSize <= totalCount) pageNo += 1;
				if(pageNo < 1) pageNo = DEFAULT_PAGE_NO;
			}
		}
	}
}
